/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pass_client.student;

import entities.Assignment;
import entities.SourceCode;
import entities.Submission;
import java.awt.FileDialog;
import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pasremote.PASRemote;

/**
 *
 * @author Administrator
 */
public class ProblemFrame extends javax.swing.JFrame {

    private final PASRemote bean;
    private final Long asmtID;

    /**
     * Creates new form ProblemFrame
     */
    ProblemFrame(PASRemote bean, Long asmtID) {
        initComponents();
        initUserComponents();
        this.bean = bean;
        this.asmtID = asmtID;
        List<Assignment> list = bean.getAssignments();
        Assignment asmt = null;

        for(Assignment a : list){
            System.out.println(a.getId());
            if(a.getId().equals(asmtID)){
                asmt = a;
            }
        }

        
        txtDetail.setText(asmt.getDescription());
        System.out.println("asmtID" + asmtID);
        List<Submission> sm = bean.getSubmissionsByAssignmentID(asmtID);

        DefaultTableModel model = (DefaultTableModel) tblSubmission.getModel();

        int numRows = model.getRowCount();
        while (model.getRowCount() > 0) {
            model.removeRow(0);
            tblSubmission.revalidate();
        }

        System.out.println("pppp" + bean.getLoginUserName());
        for (Submission s : sm) {
            System.out.println(s.getUserLoginName());
            if (s.getUserLoginName().equalsIgnoreCase(bean.getLoginUserName())) {
                String[] items = {s.getSubmitDate().toString(), s.getStatus()};
                model.addRow(items);
            }
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDetail = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblSubmission = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        txtFile = new javax.swing.JTextField();
        btnBrowse = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        btnSubmit = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        btnClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

        jPanel5.setLayout(new java.awt.GridLayout(1, 2));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Problem Detail"));
        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanel1.setLayout(new java.awt.BorderLayout());

        txtDetail.setColumns(20);
        txtDetail.setRows(5);
        jScrollPane2.setViewportView(txtDetail);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel5.add(jPanel1);

        jPanel2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Submission Statistics"));
        jPanel3.setLayout(new java.awt.BorderLayout());

        tblSubmission.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Time", "Result"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblSubmission);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel3, java.awt.BorderLayout.CENTER);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Submit Assignment"));
        jPanel4.setLayout(new javax.swing.BoxLayout(jPanel4, javax.swing.BoxLayout.PAGE_AXIS));

        jPanel8.setLayout(new javax.swing.BoxLayout(jPanel8, javax.swing.BoxLayout.LINE_AXIS));

        txtFile.setColumns(25);
        txtFile.setEnabled(false);
        txtFile.setMinimumSize(new java.awt.Dimension(70, 21));
        jPanel8.add(txtFile);

        btnBrowse.setText("Browse..");
        btnBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseActionPerformed(evt);
            }
        });
        jPanel8.add(btnBrowse);

        jPanel4.add(jPanel8);

        jPanel7.setLayout(new java.awt.BorderLayout());

        btnSubmit.setText("Submit");
        btnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitActionPerformed(evt);
            }
        });
        jPanel7.add(btnSubmit, java.awt.BorderLayout.CENTER);

        jPanel4.add(jPanel7);

        jPanel2.add(jPanel4, java.awt.BorderLayout.SOUTH);

        jPanel5.add(jPanel2);

        getContentPane().add(jPanel5);

        jPanel6.setMaximumSize(new java.awt.Dimension(32767, 50));
        jPanel6.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });
        jPanel6.add(btnClose);

        getContentPane().add(jPanel6);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseActionPerformed
        FileDialog fd = new FileDialog(this, "Open Source File", FileDialog.LOAD);
        fd.setFilenameFilter(new FilenameFilter() {

            @Override
            public boolean accept(File file, String string) {
                return string.endsWith(".java");
            }
        });
        fd.setVisible(true);
        String filename = fd.getDirectory() + fd.getFile();
        txtFile.setText(filename);

    }//GEN-LAST:event_btnBrowseActionPerformed

    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
        try {
            Submission submission = new Submission();
            submission.setAssignmentID(asmtID);
            File f = new File(txtFile.getText());

            FileReader fr = new FileReader(f);
            char c[] = new char[(int) f.length()];
            fr.read(c);
            String contents = new String(c);
            
            SourceCode srcCode = new SourceCode(contents);
            submission.setSourceCode(srcCode);
            submission.setUserLoginName(bean.getLoginUserName());
            submission.setSourceLanguage("JAVA");
            if(bean.submit(submission)){
                JOptionPane.showMessageDialog(this, "Submit Success!");
            }          
            else{
                JOptionPane.showMessageDialog(this, "Submit Fail!");
            }
        } catch (IOException ex) {
            Logger.getLogger(ProblemFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_btnSubmitActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBrowse;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblSubmission;
    private javax.swing.JTextArea txtDetail;
    private javax.swing.JTextField txtFile;
    // End of variables declaration//GEN-END:variables

    private void initUserComponents() {
        setLocationByPlatform(true);
        setSize(820, 595);
        setVisible(true);
    }
}
